package com.bw.controller;

import com.alibaba.excel.EasyExcel;
import com.bw.domain.SysDictData;
import com.bw.domain.SysDictType;
import com.bw.domain.SysPost;
import com.bw.service.SysDictDataService;
import com.bw.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/dictData")
public class SysDictDataController {

    @Autowired
    SysDictDataService sysDictDataService;

    @RequestMapping("/list")
    public Result list(@RequestBody SysDictData sysDictData){

        List<SysDictType> list=sysDictDataService.getlist(sysDictData);
        return Result.success(list);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody SysDictData sysDictData){
        sysDictData.setCreateTime(new Date());
        boolean b=sysDictDataService.add(sysDictData);
        return b?Result.success("添加成功"):Result.fail("添加失败");
    }

    @RequestMapping("/update")
    public Result update(@RequestBody SysDictData sysDictData){
        //sysDictData.setUpdateTime(new Date());
        boolean b=sysDictDataService.update(sysDictData);
        return b?Result.success("修改成功"):Result.fail("修改失败");
    }
    @RequestMapping("/export")
    public void exportPostExcel(HttpServletResponse response) throws IOException {
        String fileName = URLEncoder.encode("字典类型", "UTF-8").replaceAll("\\+", "%20");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+".xlsx");
        List<SysDictData> list=sysDictDataService.list1();

        EasyExcel.write(response.getOutputStream(),SysDictData.class).sheet("字典类型").doWrite(list);

    }

    @RequestMapping("/getxlk")
    public Result getxlk(){
        List<SysDictType> list =sysDictDataService.getlist2();
        return Result.success(list);
    }
}
